<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/8/3
 * Time: 3:42 PM
 */

namespace app\zyhsapi\model;

use app\common\model\User;
use think\Db;
use think\Log;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsConsultChatRtcRecordComment extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_consult_chat_rtc_record_comment';
    protected $cacheModel = 'voice_consult.comment';


    /**
     * 获取用户背包记录
     * @param int $astrId
     * @return array|mixed
     * @throws \Exception
     */
    public function astrCommentList(int $astrId)
    {
        $page = input('page/d', 1);
        $pageData = CacheManageZyhs::getInstance()
            ->setCacheConfig($this->cacheModel, $astrId)
            ->setPage($page)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($astrId)
            {
                return $this->getInfoByIdsInData($ids, [$astrId], $sortType);
            }, 'create_time', [], 0);

        $pageData['data'] = $this->dataInit($pageData['data']);

        return $pageData;
    }

    public function saveData($data)
    {
        try
        {
            $this->insert([
                'rtc_id' => $data['rtc_id'],
                'app_type' => $data['app_type'],
                'uid' => $data['uid'],
                'astr_id' => $data['astr_id'],
                'level' => $data['level'],
                'tids' => $data['tids'],
                'comment' => $data['comment'],
                'comment_type' => 1,
                'create_time' => time(),
            ]);
            CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $data['astr_id'])->rmCache('list');
        }
        catch (\Exception $e)
        {
            Log::write('ZyhsConsultChatRtcRecordComment model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->alias('c')->whereIn('c.rtc_id', $ids);
            } else
            {
                $this->alias('c')->where(['c.astr_id' => $type[0], 'c.status' => 1]);
            }
            $data = $this->join('good_wechat.zyhs_user u', 'c.uid=u.id', 'LEFT')->column('c.*,u.nickname name,u.avatar', 'c.rtc_id');
            $uids = [];
            $zlUids = [];
            foreach ($data as $key => $val)
            {
                if ($val['comment_type'] == 3)
                {
                    $uids[] = $val['uid'];
                }
                if ($val['app_type'] == 2)
                {
                    $zlUids[] = $val['uid'];
                }
            }
            if ($uids)
            {
                $zyhsVrtualuserUser = Db::table('good_wechat.zyhs_vrtualuser_user')->whereIn('id', $uids)->column('nickname,avatar', 'id');
            }
            if ($zlUids)
            {
                $zlUser = (new User())->whereIn('id', $zlUids)->column('nickname,avatar', 'id');
            }

            foreach ($data as $key => $val)
            {
                if (($val['comment_type'] == 3) && isset($zyhsVrtualuserUser))
                {
                    $data[$key]['name'] = $zyhsVrtualuserUser[$val['uid']]['nickname'];
                    $data[$key]['avatar'] = $zyhsVrtualuserUser[$val['uid']]['avatar'];
                }
                if (($val['app_type'] == 2) && isset($zlUser))
                {
                    $data[$key]['name'] = $zlUser[$val['uid']]['nickname'];
                    $data[$key]['avatar'] = $zlUser[$val['uid']]['avatar'];
                }
            }
            return $data;
        }
        catch (\Exception $e)
        {
            Log::write('ZyhsConsultChatRtcRecordComment model => ()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return [];
        }
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['rtc_id', 'astr_id', 'uid', 'level', 'comment_type', 'create_time']);

            if (isset($itemData['tids']))
            {
                if ($itemData['tids'])
                {
                    $tids = explode(',', $itemData['tids']);
                    $data[$index]['tids'] = array_map('intval', $tids);
                } else
                {
                    $data[$index]['tids'] = [];
                }
            }
        }

        return $data;
    }
}
